
package cn.yuyin.http;

/**
 * Exceptions of this class i thrown whenever a serious error occur that should
 * result in high level application actions. Instance variables set to be public
 * so no get methods are needed. This is done to make the footprint smaller.
 * Read about use of exceptions:
 * http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html?page=1
 */
public class IMTException extends RuntimeException {

    /**
     * The error causing the exception. Read only! Only set through constructor.
     */
    private final IMTError mError;

    /**
     * Create an exception object and set the data in the object.
     *
     * @param inError
     *            The error.
     */
    public IMTException(final IMTError inError) {
        mError = inError;
    }

    /**
    * Create an exception object and register error data, including
    * the exception that caused the error.
    *
    * @param errorCode the error code 
    * @param errorData relevant data for the error. May be null.
    * @param errorObject the object where the error occurred.
    * @param errorMethod the method where the error occurred.
    * @param devMsg message for developers to know more about the error.
     * @param th The throwable that caused the error.
    */
    
    public IMTException(final IMTErrorCode errorCode, 
            final String errorData,
             final Object errorObject, 
             final String errorMethod, 
             final String devMsg, 
             final Throwable th) {
      mError = new IMTError(errorCode, errorData, errorObject, errorMethod, devMsg, th);
    }

    /**
    * Create an exception object for errors in static methods. 
    * Register error data, including the exception that caused the error.
    *
    * @param errorCode the error code 
    * @param errorData relevant data for the error. May be null.
    * @param devMsg message for developers to know more about the error.
     * @param th The throwable that caused the error.
    */
    public IMTException(final IMTErrorCode errorCode, 
            final String errorData,
             final String devMsg, 
             final Throwable th) {
      mError = new IMTError(errorCode, errorData, null, null, devMsg, th);
    }

    /**
    * Create an exception object and register error data, in this case
    * without extra data and without an exception that caused the error.
    *
    * @param errorCode the error code 
    * @param errorObject the object where the error occurred.
    * @param errorMethod the method where the error occurred.
    */
    public IMTException(final IMTErrorCode errorCode,
             final Object errorObject, 
             final String errorMethod) {
      mError = new IMTError(errorCode, errorObject, errorMethod);
    }

    /**
    * Create an exception object and register error data, in this case
    * without extra data and without an exception that caused the error.
    *
    * @param errorCode the error code 
    * @param errorObject the object where the error occurred.
    * @param errorMethod the method where the error occurred.
    * @param th The throwable that caused the error.
    */
    public IMTException(final IMTErrorCode errorCode,
             final Object errorObject, 
             final String errorMethod, 
             final Throwable th) {
      mError = new IMTError(errorCode, null, errorObject, errorMethod,null, th);
    }

    /**
    * Create an exception object and register error data, in this case
    * without an exception that caused the error.
    *
    * @param errorCode the error code 
    * @param errorData relevant data for the error. May be null.
    * @param errorObject the object where the error occurred.
    * @param errorMethod the method where the error occurred.
    * @param devMsg message for developers to know more about the error.
    */
    public IMTException(final IMTErrorCode errorCode, 
            final String errorData,
             final Object errorObject, 
             final String errorMethod, 
             final String devMsg) {
      mError = new IMTError(errorCode, errorData, errorObject, errorMethod, devMsg, null);
    }

    /**
     * The error causing this exception.
     */
    public IMTError getError() {
        return mError;
    }

    /**
     * Convert the exception to a string, mostly for debugging purposes.
     *
     * @return a descriptive string.
     */

    public final String toString() {
        return "[IMTException  error =  " + mError.toString() + "]";
    }
}
